<template>
  <div class="flex" :style="{'--theme-color': $store.settings.theme}">
    <div class="flex-align-center">
      <iphone-x class="iphone-x" style="background-color: #FFFFFF">
        <div class="flex-justify-center">
          <readonly class="top-input">
            <el-input :prefix-icon="Search" placeholder="请输入关键字搜索" readonly/>
          </readonly>
        </div>
        <el-button
            v-show="iphoneBannerList.length > 1"
            circle
            type="primary"
            :icon="ArrowRightBold"
            class="next_banner_btn"
            @click="nextBanner"
        />
        <div class="can_select banner flex-center" :class="{'active': curSelectModel === 'banner'}" @click="curSelectModel = 'banner'">
          <el-image v-if="iphoneBanner" class="full" :src="iphoneBanner?.snapUiUrl||iphoneBanner?.uiUrl"/>
          <div v-else>轮播图为空</div>
        </div>
        <div class="can_select menu flex-align-center" :class="{'active': curSelectModel === 'menu'}" @click="curSelectModel = 'menu'">
          <template v-for="item in menuList" :key="item">
            <div class="menu_item" v-if="item.status === '0'">
              <el-image :src="item?.snapUiUrl||item?.uiUrl"/>
              <div class="menu_item_name">{{ item.name }}</div>
            </div>
          </template>
        </div>
      </iphone-x>
    </div>
    <div class="flex-1 setting">
      <div class="full" v-show="curSelectModel === 'banner'">
        <div class="setting_title">
          <div>首页轮播图</div>
          <div class="flex-1"/>
          <el-button class="save_btn" type="primary" @click="handleSaveBanner">保存轮播图</el-button>
        </div>
        <el-scrollbar class="setting_content">
          <div class="setting_item">
            <div class="title">图片设置</div>
            <div class="flex-1">
              <div class="remark">建议图片尺寸：750px&nbsp;*&nbsp;340px</div>
              <template v-for="(item,index) in bannerList" :key="item">
                <div class="item_content">
                  <div class="close" @click="handleBannerClose(item,index)">
                    <el-icon class="full">
                      <CircleCloseFilled class="close-icon"/>
                    </el-icon>
                  </div>
                  <image-cropper-upload
                      ref="banner"
                      :current-img="item.uiUrl"
                      :img-height="120"
                      :img-width="264"
                      @select-img="img => item.snapUiUrl = img"
                  />
                  <div class="option">
                    <div class="option_item">
                      <div class="option_item_title">图片名称</div>
                      <el-input v-model="item.name" placeholder="请输入图片名称"/>
                    </div>
                    <div class="option_item">
                      <div class="option_item_title">图片链接</div>
                      <mobile-link-select v-model="item.routerPage"/>
                    </div>
                    <div class="option_item">
                      <div class="option_item_title">是否启用</div>
                      <el-switch v-model="item.status" active-value="0" inactive-value="1" style="--el-switch-on-color: #13ce66;"/>
                    </div>
                  </div>
                </div>
              </template>
              <el-button type="primary" class="add_btn" @click="handleBannerAdd">添加图片</el-button>
            </div>
          </div>
        </el-scrollbar>
      </div>
      <div class="full" v-show="curSelectModel === 'menu'">
        <div class="setting_title">
          <div>导航菜单</div>
          <div class="flex-1"/>
          <el-button class="save_btn" type="primary" @click="handleSaveMenu">保存导航菜单</el-button>
        </div>
        <el-scrollbar class="setting_content">
          <div class="setting_item">
            <div class="title">菜单设置</div>
            <div class="flex-1">
              <div class="remark">最多可添加10个，建议图片尺寸：100px&nbsp;*&nbsp;100px</div>
              <template v-for="(item,index) in menuList" :key="item">
                <div class="item_content item_content_menu">
                  <div class="close" @click="handleMenuClose(item,index)">
                    <el-icon class="full">
                      <CircleCloseFilled class="close-icon"/>
                    </el-icon>
                  </div>
                  <div>
                    <image-cropper-upload
                        ref="menu"
                        :current-img="item.uiUrl"
                        :img-height="65"
                        :img-width="65"
                        @select-img="img => item.snapUiUrl = img"
                    />
                  </div>
                  <div class="option">
                    <div class="option_item">
                      <div class="option_item_title">导航名称</div>
                      <el-input v-model="item.name" placeholder="请输入导航名称"/>
                    </div>
                    <div class="option_item">
                      <div class="option_item_title">导航链接</div>
                      <mobile-link-select v-model="item.routerPage"/>
                    </div>
                    <div class="option_item">
                      <div class="option_item_title">是否启用</div>
                      <el-switch v-model="item.status" active-value="0" inactive-value="1" style="--el-switch-on-color: #13ce66;"/>
                    </div>
                  </div>
                </div>
              </template>
              <el-button type="primary" class="add_btn" @click="handleMenuAdd">添加菜单</el-button>
            </div>
          </div>
        </el-scrollbar>
      </div>
    </div>
  </div>
</template>

<script setup>

import { ArrowRightBold, CircleCloseFilled, Search } from '@element-plus/icons-vue'

const curThis = getCurrentInstance()
const curSelectModel = ref('banner')
//-------------轮播图设置--------------
const bannerList = ref([])
const getBannerList = () => {
  $xl.apis.getUiByType({jointUrl: '/banner'}).then((res) => {
    bannerList.value = res.data
    if (bannerList.value.length === 0) handleBannerAdd()
  })
}
getBannerList()
const iphoneBannerIndex = ref(0)
const iphoneBannerList = computed(() => bannerList.value.filter(item => item.status === '0'))
const iphoneBanner = computed(() => iphoneBannerList.value[iphoneBannerIndex.value])
watch(iphoneBannerIndex, () => {
  console.log('bannerIndex', iphoneBannerIndex.value)
})
const handleBannerAdd = () => {
  bannerList.value.push({
    status: "0",
    uiType: "image"
  })
}
const handleBannerClose = (item, index) => {
  if (bannerList.value.length === 1) return $xl.modal.msgWarning('至少保留一项,禁止删除!')
  if (Boolean(item.id)) return $xl.modal.confirm("确定删除该轮播图吗,将无法撤回？").then(() => deleteRemoteData(item.id, "banner", index))
  bannerList.value.splice(index, 1)
  nextBanner()
}
const nextBanner = () => {
  iphoneBannerIndex.value = (iphoneBannerIndex.value + 1) % iphoneBannerList.value.length
}
//-------------导航菜单设置--------------
const menuList = ref([])
const getMenuList = () => {
  $xl.apis.getUiByType({jointUrl: '/menu'}).then((res) => {
    menuList.value = res.data
    if (menuList.value.length === 0) handleMenuAdd()
  })
}
getMenuList()
const handleMenuAdd = () => {
  menuList.value.push({
    status: "0",
    uiType: "image"
  })
}
const handleMenuClose = (item, index) => {
  if (menuList.value.length === 1) return $xl.modal.msgWarning('至少保留一项,禁止删除!')
  if (Boolean(item.id)) return $xl.modal.confirm("确定删除该菜单吗,将无法撤回？").then(() => deleteRemoteData(item.id, "menu", index))
  menuList.value.splice(index, 1)
}
//-------------保存---------------
const handleSaveBanner = () => {
  bannerList.value = bannerList.value.filter((item) => (item.uiUrl || item.snapUiUrl) && (item.name || item.routerPage))
  if (bannerList.value.length === 0) {
    bannerList.value.push({
      status: "0",
      uiType: "image"
    })
    return $xl.modal.msgWarning('请至少添加一项有效轮播图!')
  }
  bannerList.value.forEach((item) => item.snapUiUrl = undefined)
  $xl.modal.openLoading('保存中...')
  nextTick(async () => {
    for (let i = 0 ; i < curThis.refs.banner.length ; i++) {
      const upload = curThis.refs.banner[i]
      bannerList.value[i].uiUrl = await upload.uploadImage()
    }
    $xl.apis.saveOrUpdateUiList({
      type: 'json',
      jointUrl: '/banner',
      params: bannerList.value
    }).then(() => {
      getBannerList()
      $xl.modal.closeLoading()
      $xl.modal.msgSuccess('保存成功!')
    })
  })
}
const handleSaveMenu = () => {
  menuList.value = menuList.value.filter((item) => (item.uiUrl || item.snapUiUrl) && (item.name || item.routerPage))
  if (menuList.value.length === 0) {
    menuList.value.push({
      status: "0",
      uiType: "image"
    })
    return $xl.modal.msgWarning('请至少添加一项有效菜单!')
  }
  menuList.value.forEach((item) => item.snapUiUrl = undefined)
  $xl.modal.openLoading('保存中...')
  nextTick(async () => {
    for (let i = 0 ; i < curThis.refs.menu.length ; i++) {
      const upload = curThis.refs.menu[i]
      menuList.value[i].uiUrl = await upload.uploadImage()
    }
    $xl.apis.saveOrUpdateUiList({
      type: 'json',
      jointUrl: '/menu',
      params: menuList.value
    }).then(() => {
      getMenuList()
      $xl.modal.closeLoading()
      $xl.modal.msgSuccess('保存成功!')
    })
  })
}
//-------------删除---------------
const deleteRemoteData = (id, type, index) => {
  $xl.apis.deleteUi({jointUrl: '/' + id}).then(() => {
    $xl.modal.msgSuccess('删除成功！')
    if (type === 'banner') bannerList.value.splice(index, 1)
    else menuList.value.splice(index, 1)
  }).catch(error => $xl.modal.msgError('删除失败' + error.message))
}
</script>
<style lang="scss" scoped>
.setting {
  position: relative;

  .setting_title {
    display: flex;
    align-items: center;
    height: 25px;
    font-size: 16px;

    &::before {
      content: "";
      display: inline-block;
      width: 5px;
      height: 100%;
      margin-right: 10px;
      background-color: var(--theme-color);
    }
  }

  .save_btn {
    margin-right: 10px;
  }
}

.setting_content {
  position: absolute;
  top: 25px;
  font-size: 14px;
  margin-top: 13px;
  width: 100%;
  height: calc(100% - 25px - 20px);
  border: 1px solid #E6E6E6;
  border-radius: 6px;
  padding-left: 15px;

  :deep(.el-scrollbar__view) {
    padding-bottom: 70px;
  }

  .setting_item {
    display: flex;
    margin-top: 6px;

    .title {
      font-size: 16px;
      color: #606266;
      margin-right: 10px;
      height: 30px;
      line-height: 30px;
    }

    .remark {
      width: 100%;
      font-size: 14px;
      height: 30px;
      line-height: 30px;
    }

    .item_content {
      margin-top: 5px;
      display: flex;
      align-items: center;
      width: 85%;
      height: 140px;
      border-radius: 6px;
      background-color: #F5F7FA;
      padding: 8px;
      position: relative;

      $close-icon-size: 20px;

      &:hover {
        .close {
          opacity: 1;
        }
      }

      .close {
        opacity: 0;
        transition: opacity 0.21s ease-in-out;
        position: absolute;
        cursor: pointer;
        top: - calc($close-icon-size / 2);
        right: -  calc($close-icon-size / 2);
        width: $close-icon-size;
        height: $close-icon-size;

        .close-icon {
          width: $close-icon-size;
          height: $close-icon-size;
          color: #909399;
        }
      }

      .option {
        height: 100%;
        display: flex;
        flex-wrap: wrap;
        align-content: space-around;
        flex: 1;

        .option_item {
          display: flex;
          align-items: center;
          width: 100%;

          .option_item_title {
            text-wrap: nowrap;
            color: #606266;
            font-size: 14px;
            margin: 0 10px;

          }
        }
      }
    }

    .item_content_menu {
      height: 135px;
      width: 65%;
    }

    .add_btn {
      margin-top: 15px;
    }

  }
}


</style>
<style scoped lang="scss">
.top-input {
  margin-top: 10px;
  width: 95%;

  :deep(.el-input__wrapper) {
    border-radius: calc(var(--el-component-size) * 2);
  }
}

.can_select {
  cursor: pointer;
  border: 3px dotted #E6E6E6;

  transition: border-color 0.21s ease-in-out;

  &.active {
    position: relative;
    z-index: 0;
    overflow: hidden;

    &::before {
      position: absolute;
      content: "";
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      display: block;
      background-color: rgba(255, 255, 255, 0.55);
      z-index: 1;
    }

    border-color: var(--theme-color);
    border-radius: 6px;
    border: 2px solid var(--theme-color);
  }
}

.iphone-x {
  margin: 0 60px;

  .next_banner_btn {
    position: absolute;
    top: 15%;
    right: 0;
    transform: translate(110%, -50%);
    z-index: 20;
  }
}

.banner {
  width: 100%;
  height: 162px;
  margin-top: 6px;
}

.menu {
  width: 100%;
  min-height: 80px;
  display: grid;
  /* 设置最小宽度为80px,最大宽度为1fr(自适应) */
  grid-template-columns: repeat(auto-fit, minmax(50px, 1fr));
  gap: 10px;
  padding: 0 8px;

  .menu_item {
    min-height: 50px;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    .menu_item_name {
      margin-top: 5px;
      text-wrap: nowrap;
      font-size: 13px;
      color: #606266;
    }

    .el-image {
      width: 40px;
      height: 40px;
    }
  }
}
</style>
